package com.elow.testffmpeg;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.github.hiteshsondhi88.libffmpeg.ExecuteBinaryResponseHandler;
import com.github.hiteshsondhi88.libffmpeg.FFmpeg;
import com.github.hiteshsondhi88.libffmpeg.FFmpegLoadBinaryResponseHandler;
import com.github.hiteshsondhi88.libffmpeg.exceptions.FFmpegCommandAlreadyRunningException;
import com.github.hiteshsondhi88.libffmpeg.exceptions.FFmpegNotSupportedException;

import java.io.File;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    FFmpeg fFmpeg;
    Button button_cmd;
    EditText editText_cmd;
    EditText editText_result;
    Button button_lvjing;
    Button button_wenzishuiyin;


    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};

    //请求状态码
    private static int REQUEST_PERMISSION_CODE = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            loadFFMpegLibrary();
        } catch (FFmpegNotSupportedException e) {
            e.printStackTrace();
        }

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_PERMISSION_CODE);
            }
        }

        button_lvjing = findViewById(R.id.button_lvjing);
        button_cmd = findViewById(R.id.button_cmd);
        editText_cmd = findViewById(R.id.edittext_cmd);
        editText_result= findViewById(R.id.edittext_result);
        button_wenzishuiyin = findViewById(R.id.button_wenzishuiyin);

        button_cmd.setOnClickListener(this);
        button_lvjing.setOnClickListener(this);
        button_wenzishuiyin.setOnClickListener(this);

    }

    private void exec(String[] cmds)
    {
        try {
            fFmpeg.execute(cmds,new ExecuteBinaryResponseHandler() {

                @Override
                public void onStart() {}

                @Override
                public void onProgress(String message) {}

                @Override
                public void onFailure(String message) {
                    editText_result.setText("Library run failed" + message);
//                            Toast.makeText(getApplicationContext(),"Library run failed" + message, Toast.LENGTH_LONG).show();
                }

                @Override
                public void onSuccess(String message) {
                    editText_result.setText("Library run success" + message);
//                            Toast.makeText(getApplicationContext(),"Library run success" + message, Toast.LENGTH_LONG).show();
                }

                @Override
                public void onFinish() {}
            });
        } catch (FFmpegCommandAlreadyRunningException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PERMISSION_CODE) {
            for (int i = 0; i < permissions.length; i++) {
                Log.i("MainActivity", "申请的权限为：" + permissions[i] + ",申请结果：" + grantResults[i]);


            }
        }
    }

    public void loadFFMpegLibrary() throws FFmpegNotSupportedException {
        if(fFmpeg==null)
        {
            fFmpeg = FFmpeg.getInstance(this);

            fFmpeg.loadBinary(new FFmpegLoadBinaryResponseHandler() {
                @Override
                public void onFailure() {
                    Toast.makeText(getApplicationContext(),"Library failed to load", Toast.LENGTH_LONG).show();
                }

                @Override
                public void onSuccess() {
                    Toast.makeText(getApplicationContext(),"Library loaded successfully", Toast.LENGTH_LONG).show();
                }

                @Override
                public void onStart() {

                }

                @Override
                public void onFinish() {

                }
            });
        }
    }

    @Override
    public void onClick(View view) {
        String[] cmds = null;
        switch (view.getId())
        {
            case R.id.button_cmd:
                Log.i("ll&ll", getExternalCacheDir().getPath());

                File file = new File("/data/data/com.elow.testffmpeg/cache/ios1.mp4");
                if(file.exists())
                {
                    Log.i("ll&ll", "存在");
                }
                else

                {
                    Log.i("ll&ll", "不存在");
                }

                cmds = new String[]{"-i", "/data/data/com.elow.testffmpeg/cache/ios1.mp4"};
                exec(cmds);
                break;
            case R.id.button_lvjing:
                cmds =new String[]{
                                "-i",
                                "/data/data/com.elow.testffmpeg/cache/ios1.mp4",
                                "-i",
                                "/data/data/com.elow.testffmpeg/cache/aaa1.jpg",
                                "-filter_complex",
                                "[0:v] scale=2000:2000[logo]; [logo] overlay=x=0:y=0",
                                "-strict",
                                "-2",
                                "/data/data/com.elow.testffmpeg/cache/ios2.mp4"
                        };
                exec(cmds);
                break;
            case R.id.button_wenzishuiyin:
                    cmds =new String[]{
                        "-i",
                        "/data/data/com.elow.testffmpeg/cache/ios1.mp4",
                        "-vf",
                        "drawtext=text='Title of this Video':fontfile=/system/fonts/Roboto-Thin.ttf: fontcolor=red: fontsize=24:x=0:y=0",
                        "-strict",
                        "-2",
                        "/data/data/com.elow.testffmpeg/cache/ios2.mp4"
                };
                exec(cmds);
                break;
                default:
                    break;
        }
    }
}
